package com.cloud.school.app.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ClassName CacheService
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/13 21:20
 * @Version 1.0
 */
@Service
public class CacheService implements com.cloud.school.app.config.CacheService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void hashPut(String key, String field, String value) {
        if (key == null || field == null || value == null) {
            return;
        }
        stringRedisTemplate.opsForHash().put(key, field, value);
    }

    @Override
    public boolean hashIfAbsentPut(String key, String field, String value) {
        if (key == null || field == null || value == null) {
            return false;
        }
        return stringRedisTemplate.opsForHash().putIfAbsent(key, field, value);
    }

    @Override
    public String hashGet(String key, String field) {
        if (key == null || field == null) {
            return null;
        }
        Object result = stringRedisTemplate.opsForHash().get(key, field);
        if (result != null) {
            return result.toString();
        }
        return null;
    }

    @Override
    public boolean hashHasKey(String key, String field) {
        if (key == null || field == null) {
            return true;
        }
        return stringRedisTemplate.opsForHash().hasKey(key, field);
    }

    @Override
    public Set<String> hashKeys(String key) {
        if (key == null) {
            return Sets.newHashSet();
        }
        Set<Object> result = stringRedisTemplate.opsForHash().keys(key);
        return result.stream().filter(o -> o != null).map(o -> o.toString()).collect(Collectors.toSet());
    }

    @Override
    public List<String> hashValues(String key) {
        if (key == null) {
            return Lists.newArrayList();
        }
        List<Object> result = stringRedisTemplate.opsForHash().values(key);
        return result.stream().filter(o -> o != null).map(o -> o.toString()).collect(Collectors.toList());
    }

    @Override
    public void hashDelete(String key, String field) {
        if (key == null || field == null) {
            return;
        }
        stringRedisTemplate.opsForHash().delete(key, field);
    }

    @Override
    public void hashIncr(String key, String field) {
        if (key == null || field == null) {
            return;
        }
        stringRedisTemplate.opsForHash().increment(key, field, 1);
    }

    @Override
    public void put(String key, String value) {
        if (key == null || value == null) {
            return;
        }
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void put(String key, String value, long time, TimeUnit timeUnit) {
        if (key == null || value == null) {
            return;
        }
        stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
    }

    @Override
    public boolean putIfAbsent(String key, String value, long time, TimeUnit timeUnit) {
        if (key == null || value == null || timeUnit == null) {
            return false;
        }
        return stringRedisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
    }

    @Override
    public boolean expire(String key, long timeout, TimeUnit timeUnit) {
        if (key == null || timeUnit == null) {
            return false;
        }
        return stringRedisTemplate.expire(key, timeout, timeUnit);
    }

    @Override
    public String get(String key) {
        if (key == null) {
            return null;
        }
        return stringRedisTemplate.opsForValue().get(key);
    }

    public boolean hasKey(String key) {
        if (key == null) {
            return false;
        }
        return stringRedisTemplate.hasKey(key);
    }

    @Override
    public boolean delete(String key) {
        if (key == null) {
            return true;
        }
        return stringRedisTemplate.delete(key);
    }

    @Override
    public void setPut(String key, String value) {
        if (key == null || value == null) {
            return;
        }
        stringRedisTemplate.opsForSet().add(key, value);
    }

    @Override
    public void setRemove(String key, String value) {
        if (key == null || value == null) {
            return;
        }
        stringRedisTemplate.opsForSet().remove(key, value);
    }

    @Override
    public boolean setContains(String key, String value) {
        if (key == null || value == null) {
            return false;
        }
        return stringRedisTemplate.opsForSet().isMember(key, value);
    }

    @Override
    public Set<String> setMembers(String key) {
        if (key == null) {
            return Sets.newHashSet();
        }
        return stringRedisTemplate.opsForSet().members(key);
    }
}
